﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using DMC.Database;
using System.Data.SqlClient;
using System.Data;

namespace prjInBienBanXetDuyetHoSoCapGCNNew
{
    class ClsThongTinBienBanXetDuyet
    {
        int iTotalFields = 0;
        string strDuoiWord = "";
        DataTable dtPhuong = new DataTable();
        DataTable dtNgayXetDuyet = new DataTable();
        DataTable dtChuSuDung = new DataTable();
        DataTable dtHoiDongXetDuyet = new DataTable();
        DataTable dtThuatDat = new DataTable();
        DataTable dtNguonGoc = new DataTable();
        DataTable dtTaiSanKhac = new DataTable();
        //string HienTrang = "";
        //string PhamVi = "";
        //string CanhBaoTranChap = "";
        //string DienTichO = "";
        //string DienTichNN = "";
        //string DienTichKhongDuDK = "";
        // hiện trạng
        // phạm vi
        // cảnh báo
        // diện tích ở
        // diện tích nông nghiệp
        // diện tích không đủ điều kiện
        DataTable dtNhaO = new DataTable();
        public void Creat_Word_BienBanXetDuyetNew(string vMaHoSoCapGCN,string vstrConnection,string vHienTrang,string vPhamVi,string vCanhBaoChanhChap,string vDienTichO, string vDienTichNN, string vDienTichKhongDuDK,string vTaiSanKhac)
        {
            GetThongTinBienBanHoSoXetDuyet(vMaHoSoCapGCN, vstrConnection);
            Object oMissing = System.Reflection.Missing.Value;
            Object oTrue = true;
            Object oFalse = false;
            Object oEndOfDoc = "\\endofdoc";
            Word.Application oWord = new Word.Application();
            Word.Document oWordDoc = new Word.Document();            
            oWord.Visible = true;
            //Object oTemplatePath = "C:\\CreditRequestFormTemplate.dot";
            Object oTemplatePath = System.Windows.Forms.Application.StartupPath + @"\temp\BienBanXetDuyetNew.doc";
            oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);            
            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {

                iTotalFields++;
                Word.Range rngFieldCode = myMergeField.Code;
                String fieldText = rngFieldCode.Text;

                if (fieldText.StartsWith(" MERGEFIELD"))
                {
                    Int32 endMerge = fieldText.IndexOf("\\");
                    Int32 fieldNameLength = fieldText.Length - endMerge;
                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    fieldName = fieldName.Trim();
                    //MessageBox.Show(fieldName.ToString());
                    if (fieldName == "TenPhuong")
                    {
                        myMergeField.Select();
                        if (dtPhuong.Rows.Count > 0)
                        {
                            if (dtPhuong.Rows[0]["Ten"].ToString() == "")
                            {
                                oWord.Selection.TypeText(".....");
                            }
                            else
                            {
                                oWord.Selection.TypeText(dtPhuong.Rows[0]["Ten"].ToString());
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "TenPhuongHoa")
                    {
                        myMergeField.Select();
                        if (dtPhuong.Rows.Count > 0)
                        {
                            if (dtPhuong.Rows[0]["Ten"].ToString() == "")
                            {
                                oWord.Selection.TypeText(".....");
                            }
                            else
                            {
                                string str = dtPhuong.Rows[0]["Ten"].ToString();
                                oWord.Selection.TypeText(str.ToUpper());
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "NgayXetDuyet")
                    {
                        myMergeField.Select();
                        if (dtNgayXetDuyet.Rows.Count > 0)
                        {
                            if (dtNgayXetDuyet.Rows[0]["NgayXetDuyet"].ToString() != "")
                            {
                                DateTime dtime = DateTime.Parse(dtNgayXetDuyet.Rows[0]["NgayXetDuyet"].ToString());
                                //oWord.Selection.TypeText(dtime.Hour + " giờ " + dtime.Minute + " phút Ngày " + dtime.Day + " tháng " + dtime.Month + " năm " + dtime.Year);
                                oWord.Selection.TypeText("Ngày " + dtime.Day + " tháng " + dtime.Month + " năm " + dtime.Year);
                            }
                            else oWord.Selection.TypeText("..... giờ .... phút .... Ngày .... tháng .... năm");
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "ChuSuDung")
                    {
                        myMergeField.Select();
                        if (dtChuSuDung.Rows.Count > 0)
                        {                            
                            for (int i = 0; i < dtChuSuDung.Rows.Count; i++)
                            {                                
                                string str = "";
                                if (i > 0) oWord.Selection.TypeText(" và ");
                                str = dtChuSuDung.Rows[i]["QuanHe"].ToString();
                                str = str + " " + dtChuSuDung.Rows[i]["HoTen"].ToString();
                                if (str != "")
                                {
                                    oWord.Selection.TypeText(str);
                                }
                                else oWord.Selection.TypeText("..........");
                            }
                        }
                        else oWord.Selection.TypeText("..........");
                        continue;
                    }
                    if (fieldName == "SoThua")
                    {
                        myMergeField.Select();
                        if (dtThuatDat.Rows.Count > 0)
                        {
                            if (dtThuatDat.Rows[0]["SoThua"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtThuatDat.Rows[0]["SoThua"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText("....");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "ToBanDo")
                    {
                        myMergeField.Select();
                        if (dtThuatDat.Rows.Count > 0)
                        {
                            if (dtThuatDat.Rows[0]["ToBanDo"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtThuatDat.Rows[0]["ToBanDo"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText("....");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "DiaChiThua")
                    {
                        myMergeField.Select();
                        if (dtChuSuDung.Rows.Count > 0)
                        {
                            if (dtChuSuDung.Rows[0]["DiaChi"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtChuSuDung.Rows[0]["DiaChi"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText("...............");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "HoiDongXetDuyet")
                    {
                        myMergeField.Select();
                        if (dtHoiDongXetDuyet.Rows.Count > 0)
                        {
                            int r = 0;
                            int c = 0;
                            r = dtHoiDongXetDuyet.Rows.Count;
                            c = dtHoiDongXetDuyet.Columns.Count;
                            //string str = "";
                            //for (int i = 0; i < dtHoiDongXetDuyet.Rows.Count; i++)
                            //{
                            //    str = "\n+Ông(bà) :" + dtHoiDongXetDuyet.Rows[i]["HoTen"].ToString() + "\t" + dtHoiDongXetDuyet.Rows[i]["ChucDanh"].ToString() + "-" + dtHoiDongXetDuyet.Rows[i]["ChucVu"].ToString() + ";";
                            //    if (str != "")
                            //    {
                            //        oWord.Selection.TypeText(str);
                            //    }
                            //    else
                            //    {
                            //        oWord.Selection.TypeText("........................................");
                            //    }
                            //}   
                            oWord.Selection.TypeText(" ");
                            Word.Table oTable;
                            object start = oWord.Selection.End;
                            object end = oWord.Selection.End;
                            //Word.Range wrdRng = oWordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                            Word.Range wrdRng = oWordDoc.Range(ref start,ref end);
                            oTable = oWordDoc.Tables.Add(wrdRng, r, c, ref oMissing, ref oMissing);
                            oTable.Range.ParagraphFormat.SpaceAfter = 1;                            
                            for (int i = 1; i <= r; i++)
                            {
                                for (int j = 1; j <= c; j++)
                                {
                                    //oTable.Cell(r, c).Range.Text = dtHoiDongXetDuyet.Rows[i - 1][j - 1].ToString();
                                    oTable.Cell(i, j).Range.Text = dtHoiDongXetDuyet.Rows[i - 1][j - 1].ToString();
                                }
                                oTable.Rows[i].Range.Font.Bold = 0;
                                oTable.Rows[i].Range.Font.Italic = 1;
                            }

                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "ChuSuDungFull")
                    {
                        myMergeField.Select();
                        if (dtChuSuDung.Rows.Count > 0)
                        {
                            string str = "";
                            for (int i = 0; i < dtChuSuDung.Rows.Count; i++)
                            {
                                str = "\n- " + dtChuSuDung.Rows[i]["QuanHe"].ToString() + " " + dtChuSuDung.Rows[i]["HoTen"].ToString() + " Năm sinh: " + dtChuSuDung.Rows[i]["NamSinh"].ToString() + " " + dtChuSuDung.Rows[i]["DinhDanh"].ToString() + " " + dtChuSuDung.Rows[i]["SoDinhDanh"].ToString() + "\n"
                                    + "Hộ khẩu thường trú: " + dtChuSuDung.Rows[i]["DiaChi"].ToString() + ".";
                                if (str != "")
                                {
                                    oWord.Selection.TypeText(str);
                                }
                                else
                                {
                                    oWord.Selection.TypeText("........................................");
                                }
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "DienTichRieng")
                    {
                        myMergeField.Select();
                        if (dtThuatDat.Rows.Count > 0)
                        {
                            if (dtThuatDat.Rows[0]["DienTichRieng"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtThuatDat.Rows[0]["DienTichRieng"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText("....");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "NguonGocDat")
                    {
                        myMergeField.Select();
                        if (dtNguonGoc.Rows.Count > 0)
                        {
                            if (dtNguonGoc.Rows[0]["ChiTiet"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtNguonGoc.Rows[0]["ChiTiet"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText(".........................................................");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "TaiSanKhac")
                    {
                        myMergeField.Select();
                        if (dtTaiSanKhac.Rows.Count > 0)
                        {
                            if (dtTaiSanKhac.Rows[0]["TaiSanKhac"].ToString() != "")
                            {
                                oWord.Selection.TypeText(dtTaiSanKhac.Rows[0]["TaiSanKhac"].ToString());
                            }
                            else
                            {
                                oWord.Selection.TypeText(".........................................................");
                            }
                        }
                        else oWord.Selection.TypeText(".....");
                        continue;
                    }
                    if (fieldName == "HienTrang")
                    {
                        myMergeField.Select();
                        if (vHienTrang == "") vHienTrang = "........................................................................";
                        oWord.Selection.TypeText(vHienTrang);                       
                        continue;
                    }
                    if (fieldName == "PhamViHaTang")
                    {
                        myMergeField.Select();
                        if (vPhamVi == "") vPhamVi = "........................................................................";
                        oWord.Selection.TypeText(vPhamVi);
                        continue;
                    }
                    if (fieldName == "CanhBaoChanhChap")
                    {
                        myMergeField.Select();
                        if (vCanhBaoChanhChap == "") vCanhBaoChanhChap = "........................................................................";
                        oWord.Selection.TypeText(vCanhBaoChanhChap);
                        continue;
                    }
                    if (fieldName == "DienTichDatO")
                    {
                        myMergeField.Select();
                        if (vDienTichO == "") vDienTichO = "....";
                        oWord.Selection.TypeText(vDienTichO);
                        continue;
                    }
                    if (fieldName == "DienTichNN")
                    {
                        myMergeField.Select();
                        if (vDienTichNN == "") vDienTichNN = "....";
                        oWord.Selection.TypeText(vDienTichNN);
                        continue;
                    }
                    if (fieldName == "DienTichKhongDuDK")
                    {
                        myMergeField.Select();
                        if (vDienTichKhongDuDK == "") vDienTichKhongDuDK = "....";
                        oWord.Selection.TypeText(vDienTichKhongDuDK);
                        continue;
                    }
                    if (fieldName == "ThongTinNha")
                    {
                        myMergeField.Select();
                        if (dtNhaO.Rows.Count > 0)
                        {
                            //string str = "";
                            //for (int i = 0; i < dtNhaO.Rows.Count; i++)
                            //{
                            //    str = "\nLoại nhà: " + dtNhaO.Rows[i]["LoaiNha"].ToString() + " kết cấu " + dtNhaO.Rows[i]["KetCauNha"].ToString() + " ,số tầng " + dtNhaO.Rows[i]["SoTang"].ToString() + " \nDiện tích nhà ở đề nghị cấp giấy GCNQSH nhà(diện tích sử dụng):" + dtNhaO.Rows[i]["DienTichSanXayDung"].ToString()+" m2.";
                            //    if (str != "")
                            //    {
                            //        oWord.Selection.TypeText(str);
                            //    }
                            //    else
                            //    {
                            //        oWord.Selection.TypeText(".....................................................");
                            //    }
                            //}
                            int r = 0;
                            int c = 0;
                            r = dtNhaO.Rows.Count + 1;
                            c = dtNhaO.Columns.Count+1;
                            //string str = "";
                            //for (int i = 0; i < dtHoiDongXetDuyet.Rows.Count; i++)
                            //{
                            //    str = "\n+Ông(bà) :" + dtHoiDongXetDuyet.Rows[i]["HoTen"].ToString() + "\t" + dtHoiDongXetDuyet.Rows[i]["ChucDanh"].ToString() + "-" + dtHoiDongXetDuyet.Rows[i]["ChucVu"].ToString() + ";";
                            //    if (str != "")
                            //    {
                            //        oWord.Selection.TypeText(str);
                            //    }
                            //    else
                            //    {
                            //        oWord.Selection.TypeText("........................................");
                            //    }
                            //}   
                            oWord.Selection.TypeText(" ");
                            Word.Table oTable;
                            object start = oWord.Selection.End;
                            object end = oWord.Selection.End;
                            //Word.Range wrdRng = oWordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                            Word.Range wrdRng = oWordDoc.Range(ref start, ref end);
                            oTable = oWordDoc.Tables.Add(wrdRng, r, c, ref oMissing, ref oMissing);
                            oTable.Range.ParagraphFormat.SpaceAfter = 1;
                            oTable.Cell(1, 1).Range.Text = "Hạng mục hoặc căn hộ số";
                            oTable.Cell(1, 2).Range.Text = "Loại nhà";
                            oTable.Cell(1, 3).Range.Text = "Kết cấu nhà";
                            oTable.Cell(1, 4).Range.Text = "Số Tầng";
                            oTable.Cell(1, 5).Range.Text = "Diện tích sàn xây dựng";                            
                            for(int i=1;i<=r;i++)
                                for (int j = 1; j <= c; j++)
                                {
                                    oTable.Cell(i, j).Borders.Enable = 1;
                                    //oTable.Cell(i, j).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;                                
                                }
                            for (int i = 2; i <= r; i++)
                            {
                                for (int j = 1; j <= c; j++)
                                {
                                    //oTable.Cell(r, c).Range.Text = dtHoiDongXetDuyet.Rows[i - 1][j - 1].ToString();
                                    if (j == 1) oTable.Cell(i, j).Range.Text = (i - 1).ToString();
                                    else
                                    {
                                        oTable.Cell(i, j).Range.Text = dtNhaO.Rows[i - 2][j - 2].ToString();
                                    }                                                                      
                                    //oTable.Cell(i, j).Borders.DistanceFromBottom = 1;
                                }
                                oTable.Rows[i].Range.Font.Bold = 0;
                                oTable.Rows[i].Range.Font.Italic = 1;
                            }
                        }
                        else oWord.Selection.TypeText("....");
                        continue;
                    }

                }
            }            
        }
        public void GetThongTinBienBanHoSoXetDuyet(string vMaHoSoCapGCN,string vstrConnection)
        {
            string[] paras = { "@MaHoSoCapGCN"};
            string[] values = { vMaHoSoCapGCN};
            clsDatabase clsdata = new clsDatabase();
            clsdata.Connection = vstrConnection;
            clsdata.OpenConnection();
            // lấy tên phường
            clsdata.getValue(ref dtPhuong, "CT_SelectTenPhuongByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtNgayXetDuyet, "CT_SelectNgayXetDuyetByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtChuSuDung, "CT_SelectChuSuDungByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtHoiDongXetDuyet, "CT_SelectHoiDongXetDuyetByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtThuatDat, "CT_SelectThongTinDatByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtNguonGoc, "CT_SelectNguonGocByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtNhaO, "CT_SelectNhaOByMaHoSoCapGCN", paras, values);
            clsdata.getValue(ref dtTaiSanKhac, "CT_SelectTaiSanKhacByMaHoSoCapGCN", paras, values);
            clsdata.CloseConnection();
        }                   
    }
}
